/*
 * Copyright 2019-present HiveMQ GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hivemq.extensions.rbac.redis.configuration.entities;

import org.jetbrains.annotations.NotNull;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * 扩展配置
 */
@XmlRootElement(name = "extension-configuration")
@XmlType(propOrder = {})
@XmlAccessorType(XmlAccessType.FIELD)
public class ExtensionRedisConfig {

    @XmlElement(name = "credentials-reload-interval", defaultValue = "60")
    private int reloadInterval = 60;

    @XmlElement(name = "next-extension-instead-of-fail", defaultValue = "true")
    private boolean nextExtensionInsteadOfFail = false;

    @XmlElement(name = "redis-host")
    private @NotNull String redisHost;

    @XmlElement(name = "redis-port", defaultValue = "6379")
    private int redisPort = 6379;
    @XmlElement(name = "redis-db", defaultValue = "-1")
    private int redisDb = -1;
    @XmlElement(name = "redis-password", defaultValue = "")
    private String redisPassword = "";

    /** 超时时间 */
    @XmlElement(name = "redis-timeout", defaultValue = "60")
    private int redisTimeOut;
    /** Redis可连接实例最大数目 */
    @XmlElement(name = "redis-maxtotal", defaultValue = "100")
    private int redisMaxTotal;
    /** Redis最多有多少个状态为idle(空闲)的jedis实例，默认值是8 */
    @XmlElement(name = "redis-maxidle", defaultValue = "20")
    private int redisMaxIdle;
    /** Reids等待可用连接的最大时间，单位是毫秒，默认值为-1，表示永不超时。 **/
    @XmlElement(name = "redis-maxwaitnillis", defaultValue = "120")
    private int redisMaxWaitNillis;
    /** 在borrow(用)一个jedis实例时，是否提前进行validate(验证)操作 */
    @XmlElement(name = "redis-testonborrow", defaultValue = "true")
    private boolean redisTestOnBorrow;

    public ExtensionRedisConfig() {
    }

    public ExtensionRedisConfig(
            final int reloadInterval,
            final boolean nextExtensionInsteadOfFail,
            final @NotNull String redisHost,
            final int redisPort,
            final int redisDb,
            final @NotNull String redisPassword,
            final int redisTimeOut,
            final int redisMaxTotal,
            final int redisMaxIdle,
            final int redisMaxWaitNillis,
            final boolean redisTestOnBorrow
    ) {
        this.reloadInterval = reloadInterval;
        this.redisHost = redisHost;
        this.redisPort = redisPort;
        this.redisDb = redisDb;
        this.redisPassword = redisPassword;
        this.nextExtensionInsteadOfFail = nextExtensionInsteadOfFail;

        this.redisTimeOut = redisTimeOut;
        this.redisMaxTotal = redisMaxTotal;
        this.redisMaxIdle = redisMaxIdle;
        this.redisMaxWaitNillis = redisMaxWaitNillis;
        this.redisTestOnBorrow = redisTestOnBorrow;
    }

    public int getReloadInterval() {
        return reloadInterval;
    }

    public void setReloadInterval(final int reloadInterval) {
        this.reloadInterval = reloadInterval;
    }

    public boolean isNextExtensionInsteadOfFail() {
        return nextExtensionInsteadOfFail;
    }

    public @NotNull String getRedisHost() {
        return redisHost;
    }

    public void setRedisHost(final @NotNull String redisHost) {
        this.redisHost = redisHost;
    }

    public int getRedisPort() {
        return redisPort;
    }

    public void setRedisPort(final int redisPort) {
        this.redisPort = redisPort;
    }

    public int getRedisDb() {
        return redisDb;
    }

    public void setRedisDb(final int redisDb) {
        this.redisDb = redisDb;
    }

    public String getRedisPassword() {
        return redisPassword;
    }

    public void setRedisPassword(final String redisPassword) {
        this.redisPassword = redisPassword;
    }

    public int getRedisTimeOut() {
        return redisTimeOut;
    }

    public void setRedisTimeOut(final int redisTimeOut) {
        this.redisTimeOut = redisTimeOut;
    }

    public int getRedisMaxTotal() {
        return redisMaxTotal;
    }

    public void setRedisMaxTotal(final int redisMaxTotal) {
        this.redisMaxTotal = redisMaxTotal;
    }

    public int getRedisMaxIdle() {
        return redisMaxIdle;
    }

    public void setRedisMaxIdle(final int redisMaxIdle) {
        this.redisMaxIdle = redisMaxIdle;
    }

    public int getRedisMaxWaitNillis() {
        return redisMaxWaitNillis;
    }

    public void setRedisMaxWaitNillis(final int redisMaxWaitNillis) {
        this.redisMaxWaitNillis = redisMaxWaitNillis;
    }

    public boolean isRedisTestOnBorrow() {
        return redisTestOnBorrow;
    }

    public void setRedisTestOnBorrow(final boolean redisTestOnBorrow) {
        this.redisTestOnBorrow = redisTestOnBorrow;
    }

    @Override
    public String toString() {
        return "RedisConfig{" +
                "reloadInterval=" +
                reloadInterval +
                ", nextExtensionInsteadOfFail=" +
                nextExtensionInsteadOfFail +
                ", redisHost='" +
                redisHost +
                '\'' +
                ", redisPort=" +
                redisPort +
                ", redisDb=" +
                redisDb +
                ", redisPassword='" +
                redisPassword +
                '\'' +
                ", redisTimeOut=" +
                redisTimeOut +
                ", redisMaxTotal=" +
                redisMaxTotal +
                ", redisMaxIdle=" +
                redisMaxIdle +
                ", redisMaxWaitNillis=" +
                redisMaxWaitNillis +
                ", redisTestOnBorrow=" +
                redisTestOnBorrow +
                '}';
    }
}
